package net.pessu.android.shoppoid.gui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;

public class TextChoices extends EditText {

	Context currentContext;

	String titre = "";

	CharSequence[] items;

	DialogInterface.OnClickListener clickItem;

	public TextChoices(Context context) {
		super(context);
		this.currentContext = context;
	}

	public TextChoices(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.currentContext = context;
	}

	public TextChoices(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.currentContext = context;
	}

	public void init(String titre, CharSequence[] data,
			DialogInterface.OnClickListener click) {
		this.titre = titre;
		this.items = data;
		this.clickItem = click;
		setText(data[0]);
		setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		setMinWidth(75);
		setFocusable(false);
		setGravity(Gravity.CENTER);
		setOnClickListener(getClickListener());
		setInputType(0);
	}

	public OnClickListener getClickListener() {

		return (new OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						currentContext);
				builder.setTitle(titre);
				builder.setSingleChoiceItems(items, -1, clickItem);
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
	}

	public void changeValue(int position) {
		if (position < this.items.length && position >= 0) {
			setText(this.items[position]);
		}
	}

	/**
	 * @return the items
	 */
	public CharSequence[] getItems() {
		return items;
	}

	/**
	 * @param items
	 *            the items to set
	 */
	public void setItems(CharSequence[] items) {
		this.items = items;
	}

	/**
	 * @return the clickItem
	 */
	public DialogInterface.OnClickListener getClickItem() {
		return clickItem;
	}

	/**
	 * @param clickItem
	 *            the clickItem to set
	 */
	public void setClickItem(DialogInterface.OnClickListener clickItem) {
		this.clickItem = clickItem;
	}

}
